import pandas as pd
import os
import numpy as np


folder_path = 'D:\\accessability\\ACCESSIBILITY_RESULTS\\Model_Experiment_Result_Raw_Data'

#The list of the model name and the language
themes = [ "GPT4.1_CHINESE", "GPT4.1_ENGLISH", "Gemini_1.5_Pro_CHINESE", "Gemini_1.5_Pro_ENGLISH","GPT4O_CHINESE", "GPT4O_ENGLISH",
    "CLAUDE_CHINESE", "CLAUDE_ENGLISH","LLAMA4_CHINESE", "LLAMA4_ENGLISH", "QWEN_CHINESE", "QWEN_ENGLISH"
]

#A list to store data
statistics = []

# process each theme
for theme in themes:
    correct_list = []
    
    # ten run
    for run in range(1, 11):
        correct_count = 0
        total_count = 0
        file_name = f"{theme}_for_questions_answers_run_{run}.csv"
        file_path = os.path.join(folder_path, file_name)
        
        if os.path.exists(file_path):
            df = pd.read_csv(file_path)
            # The experiment group spans from 0 to 60.
            # The control groups are divided into four intervals: 
            # 60–120, 120–180, 180–240 and 240–300.
            df = df.iloc[0:60].reset_index(drop=True)

            #referent identification based on accessibility accuracy
            for i in range(0, len(df), 4):
                if i + 3 < len(df):
                    if (df.loc[i, 'Comparison'] == 1 and df.loc[i + 1, 'Comparison'] == 1 and
                        df.loc[i + 2, 'Comparison'] == 1 and df.loc[i + 3, 'Comparison'] == 1):
                        correct_count += 1
                    total_count += 1
             
            
            '''#proximity accuracy
            for i in range(0, len(df), 2):
                if i + 1 < len(df):
                    if df.loc[i, 'Comparison'] == 1 and df.loc[i + 1, 'Comparison'] == 1:
                        correct_count += 1
                    total_count += 1
            '''
            
            '''           
            #single question accuracy
            for i in range(len(df)):
                if df.loc[i, 'Comparison'] == 1:
                    correct_count += 1
                total_count += 1
            '''
        if total_count > 0:
            correct_list.append(correct_count / total_count)
    
    # count
    if correct_list:
        average = np.mean(correct_list)
        std_dev = np.std(correct_list)
        max_value = np.max(correct_list)
        min_value = np.min(correct_list)
        
        statistics.append({
            "Theme": theme,
            "Average ± Std Dev": f"{average:.4f} ± {std_dev:.4f}",
            "Max": max_value,
            "Min": min_value
        })

# Output the statistical data to a new file
output_df = pd.DataFrame(statistics)
output_path="D:\\accessability\\ACCESSIBILITY_RESULTS\\Model_Experiment_Result_Statistics_Data\\Acessibility_task_experiment_group"
output_file_path = os.path.join(output_path, 'Task_accessibility_accuracy_statistics.csv')  # Your file name
output_df.to_csv(output_file_path, index=False)

print(f"All statistical data has been computed and saved to the file: {os.path.basename(output_file_path)}")

